<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mdl_penentuan_dosen extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata(){
		$kd_fakultas = isset($_POST['s_kd_fakultas']) ? strval($_POST['s_kd_fakultas']) : '';
		$kd_prodi = isset($_POST['s_kd_prodi']) ? strval($_POST['s_kd_prodi']) : '';
		$tahun = isset($_POST['s_tahun']) ? strval($_POST['s_tahun']) : '';
		
		$this->db->flush_cache();
		$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, b.nama_prodi, a.kd_prodi');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		//$this->db->where('a.thn_angkatan', $dt['thn_angkatan']);
		
		#--Faklutas
		// if($this->session->userdata('kd_fakultas')!=''){
			// $this->db->where('b.kd_fakultas', $this->session->userdata('kd_fakultas'));
		// }else{
			//if($kd_fakultas != '')
				//$this->db->where('b.kd_fakultas', $kd_fakultas);
		// }
		#--Prodi
		// if($this->session->userdata('kd_prodi')!=''){
			// $this->db->where('a.kd_prodi', $this->session->userdata('kd_prodi'));
		// }else{
			//if($kd_prodi != '')
				$this->db->where('a.kd_prodi', $kd_prodi);
		// }
		
		//if($tahun != ''){
			$this->db->where('a.thn_angkatan', $tahun);
		//}
		
		$this->db->where('a.kd_dosen is null');
		$this->db->where('a.status', 'Mahasiswa Terdaftar');
		//$this->db->limit($dt['jumlah'], 0);
		$this->db->order_by('a.thn_angkatan', 'ASC');
		
		$q = $this->db->get()->result();
		
		$out = '';
		$i=1;
		$color = '';
		foreach($q as $r){
			$color = ($i % 2 == 0)?'#FFFFFF':'#fff9cf';
			$out .= '<tr>';
			$out .= '  <td bgcolor="'.$color.'">'.$i;
			$out .= '     <input type="hidden" name="data['.$i.'][thn_angkatan]" value="'.$r->thn_angkatan.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][nim]" value="'.$r->nim.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][nama]" value="'.$r->nama.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][kd_prodi]" value="'.$r->kd_prodi.'">';
			$out .= '  </td>';
			$out .= '  <td bgcolor="'.$color.'"><input type="checkbox" name="data['.$i.'][chk]"/></td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->nim.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->nama.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->nama_prodi.'</td>';
			$out .= '</tr>';
			$i++;
		}
		
		return $out;
	}
	
	function InsertOnDB($data){
		$this->db->trans_start();
		
		foreach($data['data'] as $row){
			if(isset($row['chk'])){
				if($row['nim'] != '' || $row['nim'] != null){
					$this->db->flush_cache();
					$this->db->set('kd_dosen', $data['kd_dosen']);
					$this->db->set('status', 'Mahasiswa Aktif');
					
					$this->db->where('nim', $row['nim']);
					$this->db->update('tbl_mahasiswa');
				}
			}
		}
		
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
}

?>